package com.adxon.data;

import com.adxon.MsgException;

public class ItemSet extends DataSet<Tagging>{
	private int	kdn;
	private int	rat;

	public ItemSet(Item template){
		super(template);
	}
	@Override
	public Tagging next(){
		Item r=((Tagged)nxt).extend();
		if(((Item)tmp).sto!=0)
			r.setStarer(((Item)tmp).sto);
		nxt=null;
		return r;
	}
	@Override
	protected void prepFilter(Tagging template){
		if(template.tg==null){
			bsk=Item.BSK;
			cls=Untagged.class;
			if(template.kn>0)
				addFilter("kn",template.kn);
			else kdn=-template.kn;
			addFilter("rf",((Item)template).rf);
		}else{
			bsk=Tag.BSK;
			cls=Tagged.class;
			addFilter("tg",template.tg);
		}
		addFilter("ow",template.ow);
		rat=template.rt;
		tmp=template;
	}
	@Override
	protected boolean skip(){
		if(kdn!=0 && kdn==nxt.kn)
			return true;
		if(rat!=0){
			int rt=nxt.rt;
			if(rat<100){
				if(rt>rat)
					return true;
			}else if(rt<rat)
				return true;
		}
		//System.out.println("not skip: "+nxt+", kn="+nxt.kn+", rt="+nxt.rt);
		return false;
	}
}
class Tagged extends Tagging{
	Item itm;

	Item extend(){
		try{
			if(itm==null)
				itm=(Item)new Item().update(id);
			Item r=itm;
			itm=null;
			return r.extend();
		}catch(MsgException e){
			e.printStackTrace();
			return null;
		}
	}
}
class Untagged extends Tagged{
	@Override
	Item extend(){
		try{
			@SuppressWarnings("rawtypes")
			Class c=kn<Item.CLS.length?Item.CLS[kn]:null;
			if(c==null)
				c=Item.class;
			return (Item)((Item)c.newInstance()).update(ent);
		}catch(MsgException e){
			return null;
		}catch(IllegalAccessException e){
			return null;
		}catch(InstantiationException e){
			return null;
		}
	}
	@Override
	public Untagged update()throws MsgException{
		super.update();
		kn=updateInt("kn");
		return this;
	}
}